<template>
    <el-row justify="end">
    <el-button @click="openForm">
      <Icon class="mr-5px" icon="ep:edit" />
      {{ chemicals.id ? '编辑化学品信息' : '新增化学品信息' }}
    </el-button>
            <el-button type="warning" plain @click="handleImport" v-hasPermi="['crm:chemicals:import']">
          <Icon icon="ep:upload" /> 导入
        </el-button>
  </el-row>
  <label class="font-bold">主要化学品使用信息登记表</label>
  <ChemicalsRegisterList :key="chemicalsRegisterListRefKey" ref="chemicalsRegisterListRef" :chemicals="chemicals" />
  <ContentWrap>
    <el-collapse v-model="activeNames" class="">
      <el-collapse-item name="systemInfo">
        <template #title>
          <span class="text-base font-bold">系统信息</span>
        </template>
        <el-descriptions :column="4">
          <el-descriptions-item label="创建人">{{ chemicals.creatorName }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ formatDate(chemicals.createTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="修改人">{{ chemicals.updaterName }}</el-descriptions-item>
          <el-descriptions-item label="修改时间">
            {{ formatDate(chemicals.updateTime) }}
          </el-descriptions-item>
        </el-descriptions>
      </el-collapse-item>
    </el-collapse>
  </ContentWrap>
    <!-- 表单弹窗：添加 -->
    <ChemicalsForm ref="formRef" @success="refresh" />
      <!-- 配套信息导入对话框 -->
  <ChemicalsImportForm ref="importFormRef" @success="refresh" />
</template>
<script lang="ts" setup>
import { ChemicalsApi,ChemicalsVO } from '@/api/crm/chemicals'
import { formatDate } from '@/utils/formatTime'
import ChemicalsRegisterList from '@/views/crm/chemicals/components/ChemicalsRegisterList.vue'
import ChemicalsForm from '../ChemicalsForm.vue'
import ChemicalsImportForm from '../ChemicalsImportForm.vue'
const formRef = ref()
const chemicalsRegisterListRefKey = ref(new Date().getTime())
defineOptions({ name: 'ChemicalsDetail' })
const props = defineProps<{ id?: number,bizId?: number }>()
const route = useRoute();
/** 导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}
const openForm = () => {
  formRef.value.open(chemicals.value.id ? 'update' : 'create', chemicals.value.id, props.bizId)
}
const formLoading = ref(false) // 表单的加载中
const chemicals = ref<ChemicalsVO>({} as ChemicalsVO) // 化学品信息详情

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.bizId,
  async (val) => {
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      const data = await ChemicalsApi.getChemicalsByCustomerId(val)
      if (!data) {
        return
      }
      chemicals.value = data
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)
const activeNames = ref(['basicInfo', 'systemInfo']) // 展示的折叠面板
const refresh = async () => {
  chemicalsRegisterListRefKey.value = new Date().getTime()

  try {
    formLoading.value = true
    const data = await ChemicalsApi.getChemicalsByCustomerId(props.bizId)
    if (!data) {
      return
    }
    chemicals.value = data
  } finally {
    formLoading.value = false
  }
}

/** 初始化 */
onMounted(async () => {
  const id = props.id || route.params.id
  const customerId = props.bizId
  try {
    formLoading.value = true
    if(customerId){
      const data = await ChemicalsApi.getChemicalsByCustomerId(customerId)
      if (!data) {
        return
      }
      chemicals.value = data
    } else {
      if(id){
        const data = await ChemicalsApi.getChemicals(id)
        if (!data) {
          return
        }
        chemicals.value = data
      }
    }
  } finally {
    formLoading.value = false
  }
})
</script>
